<template>
  <div class="processpanel">
    <el-dialog
      :close-on-click-modal="false"
      title="新增员工"
      :visible="dialogShow"
      @close="cancle"
    >
      <el-form ref="myform" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment">
            <el-option
              v-for="(item, index) in hireType"
              :key="index"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div class="box">
            <el-input @focus="myfocus" v-model="form.departmentName"></el-input>
            <el-tree
              v-if="showTree"
              :data="list"
              :props="{ label: 'name' }"
              class="mytree"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="nodeclick"
            ></el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正日期" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button @click="submit" type="primary">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入接口
import { getDepartmentApi } from '@/api/departments'
import { addEmployeesApi } from '@/api/employees'
// 导入枚举数据
import employeesEnus from '@/api/constant/employees'
export default {
  // dialogShow: 控制面板的显示与隐藏
  props: ['dialogShow'],
  data () {
    return {
      // 数据源
      form: {
        username: '', // 姓名
        mobile: '', // 手机
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正日期
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'blur' }],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 11, max: 11, message: '长度不正确', trigger: 'blur' },
          {
            validator: (rules, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value) ? callback() : callback(new Error('手机号不正确'))
            }
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        departmentName: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      // 选择器的数据源
      hireType: employeesEnus.hireType,
      // 部门名称的列表
      list: [],
      // 控制树状结构是否显示
      showTree: false
      //
    }
  },
  methods: {
    // 提交数据
    submit () {
      // 全局验证
      this.$refs.myform.validate(async isOK => {
        if (isOK) {
          // 提交数据
          await addEmployeesApi(this.form)
          // 其它处理
          this.$emit('update:dialogShow', false)
          this.form = {
            username: '', // 姓名
            mobile: '', // 手机
            timeOfEntry: '', // 入职时间
            formOfEmployment: '', // 聘用形式
            workNumber: '', // 工号
            departmentName: '', // 部门
            correctionTime: '' // 转正日期
          }
          // 清除表单的校验
          this.$refs.myform.resetFields()
          this.$message.success('新增成功')
          // 调用父组件中的方法
          this.$emit('updateData')
        }
      })
    },
    // 得到部门数据
    async getDepartment () {
      const res = await getDepartmentApi()
      this.list = this.trunData(res.data.depts, '')
    },
    // 转换树状结构
    trunData (arr, pid) {
      const newArr = []
      arr.forEach(item => {
        if (item.pid === pid) {
          const children = this.trunData(arr, item.id)
          if (children.length > 0) {
            item.children = children
          }
          newArr.push(item)
        }
      })
      return newArr
    },
    // 点击树状结构时会触发
    nodeclick (data) {
      this.showTree = false
      this.form.departmentName = data.name
    },
    // 部门输入框获取焦点时会触发
    myfocus () {
      this.showTree = true
    },
    // 关闭面板
    cancle () {
      this.$emit('update:dialogShow', false)
    }
  },
  created () {
    // 得到部门数据
    this.getDepartment()
  }
}
</script>

<style lang="scss">
.processpanel {
  .box {
    position: relative;
    width: 300px;
    .mytree {
      position: absolute;
      border: 1px solid #ddd;
      border-radius: 5px;
      width: 100%;
      top: 44px;
      left: 0;
      z-index: 999;
      .el-tree-node__content {
        height: 30px;
        .el-icon-caret-right {
          display: none;
        }
        .el-tree-node__label {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
